package com.club.football.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.club.football.common.UserHolder;
import com.club.football.dto.*;
import com.club.football.entity.User;
import com.club.football.req.UserPageReq;
import com.club.football.service.UserService;
import com.club.football.utils.R;
import com.club.football.vo.UserVo;
import lombok.AllArgsConstructor;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static com.club.football.common.CommonConstants.*;

/**
 * 用户Controller
 *
 * @author 1
 * @date 2023/12/28
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private UserService userService;

    /**
     * 按ID查询
     *
     * @return {@link R}<{@link UserVo}>
     */
    @GetMapping("/getUserInfo")
    public R<UserVo> queryById() {
        Long userId = UserHolder.getUser().getUserId();
        return userService.queryById(userId);
    }

    @GetMapping(value = "/getRoutes")
    public R<List<String>> getRoutes() {
        return R.ok(new ArrayList<>());
    }


    /**
     * 登录
     *
     * @param loginDto 登录数据到
     * @return {@link R}
     */
    @PostMapping("/login")
    public R<String> login(@RequestBody @Valid LoginDto loginDto) {
        return userService.login(loginDto);
    }

    /**
     * 用户分页查询
     *
     * @param pageDto 页数到页
     * @return {@link R}
     */
    @GetMapping("/page")
    public R<Page<UserVo>> page(PageDto<User> pageDto, UserPageReq pageReq) {

        return userService.userPage(pageDto, pageReq);
    }

    /**
     * 重置密码
     *
     * @param resetDto 将数据重置为
     * @return {@link R}<{@link Boolean}>
     */
    @PostMapping("/reset")
    public R<Boolean> reset(@RequestBody ResetDto resetDto) {
        userService.lambdaUpdate()
                .eq(User::getUserId, resetDto.getUserId())
                .set(User::getPwd, DigestUtils.md5DigestAsHex(DEFAULT_PASSWORD.getBytes()))
                .update();
        return R.ok(OPERATION_OK);
    }

    /**
     * 删除用户
     *
     * @param id ID
     * @return {@link R}<{@link String}>
     */
    @DeleteMapping("/remove/{id}")
    public R<String> remove(@PathVariable Long id) {
        if (ADMIN_ID == id) {
            return R.failed(DISALLOWED_OPERATION);
        }
        userService.removeById(id);
        return R.ok(DEL_OK_MSG);
    }

    /**
     * 保存用户
     *
     * @param userSaveDto 用户将数据保存到
     * @return {@link R}<{@link String}>
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody @Valid UserSaveDto userSaveDto) {
        return userService.saveUser(userSaveDto);
    }

    /**
     * 更改密码
     *
     * @param dto DTO
     * @return {@link R}<{@link String}>
     */
    @PostMapping("/changePassword")
    public R<String> changePwd(@RequestBody @Valid ChangePwdDto dto) {
        return userService.changePwd(dto);
    }

    /**
     * 设置电话
     *
     * @param dto DTO
     * @return {@link R}<{@link String}>
     */
    @PutMapping("/changePhone")
    public R<String> setPhone(@RequestBody UserSaveDto dto) {
        return userService.changePhone(dto);
    }


}
